package com.example.scheduled;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.example.properties.DemoProperties;

/**
 * 定时任务
 * 
 * @author lli
 *
 * @version 1.0
 * 
 */
@Service
public class TestScheduled {
    private final static Logger logger = LoggerFactory.getLogger(TestScheduled.class);
    private final ExecutorService ex = Executors.newFixedThreadPool(10, new MyThreadFactory(
            "testpool"));
    /**
     * 配置资源
     */
    @Resource
    DemoProperties demoProperties;

    @Value("${test.id}")
    private double testId;

    // @Scheduled(cron = "0/1 * * * * ?")
    // @Scheduled(fixedRate = 5000) ：上一次开始执行时间点之后5秒再执行
    // @Scheduled(fixedDelay = 5000) ：上一次执行完毕时间点之后5秒再执行
    // @Scheduled(initialDelay=1000,
    // fixedRate=5000)：第一次延迟1秒后执行，之后按fixedRate的规则每5秒执行一次每1秒执行一次
    public void test() {
        // System.out.println(testId);

    }

    // @Scheduled(fixedDelay = 5000)
    public void tes2() {
        logger.info("定时任务2.");
        ex.isShutdown();

    }

    class MyThreadFactory implements ThreadFactory {
        private int counter;
        private String name;

        public MyThreadFactory(String name) {
            counter = 1;
            this.name = name;
        }

        @Override
        public Thread newThread(Runnable runnable) {
            Thread t = new Thread(runnable, name + "-Thread_" + counter);
            counter++;
            return t;
        }

    }
}
